package robocup.component.util;

import robocup.component.WorldModel;


/**
 * @author Thomas Karbe
 * 
 * collects messages and prints them or deletes them
 *
 */
public class DebugPrinter {

  StringBuffer sb;
  WorldModel wm;
  
  /**
   * Creates an empty DebugPrinter
   */
  public DebugPrinter() {
    sb= new StringBuffer();
    wm= null;
  }
  
  /**
   * Creates an empty DebugPrinter
   * @param wm the Worldmodel
   */
  public DebugPrinter(WorldModel wm) {
    sb= new StringBuffer();
    this.wm= wm;
  }
  
  /**
   * Deletes all saved contents of the DebugPrinter
   */
  public void clean() {
    sb.setLength(0);
  }
  
  /**
   * adds a new Line
   */
  public void addNewLine() {
      sb.append("\n");
  }
  
  /**
   * Adds the given text to a new line (if wm is given, the cycle and player string is added)
   * @param s the String to add
   */
  public void addTextOnNewLine(Object s) {
    if (wm == null) {
      sb.append("\n").append(s.toString()); 
    }
    else {
      sb.append("\n").append(wm.getCycleAndPlayerString()).append(' ').append(s==null?"NULL":s.toString());
    }
  }
  
  /**
   * Appends the given String to the end.
   * @param s the String to add
   */
  public void addText(Object s) {
    sb.append(s==null?"NULL":s.toString());
  }
  
  /**
   * prints the collected messages to System.out and deletes them afterwards
   */
  public void printMessages() {
    System.out.println(sb.toString());
    this.clean();
  }
}
